﻿using Dapper_Core.Model;
using System;
using System.Collections.Generic;
using System.Text;

namespace Dapper_Core.Common
{
    public class EntityComparer
    {
        /// <summary>
        /// 进行比较并返回有变动的字段信息
        /// </summary>
        /// <returns></returns>
        public static List<ColumnChange> Compare(object beforeEntity, object afterEntity)
        {
            List<ColumnChange> result = new List<ColumnChange>();
            if (beforeEntity == null || afterEntity == null)
            {

            }
            else if (beforeEntity.Equals(DBNull.Value) || afterEntity.Equals(DBNull.Value))
            {

            }
            else if (beforeEntity.GetType() != afterEntity.GetType())
            {

            }
            else if (beforeEntity is int || beforeEntity is short || beforeEntity is long || beforeEntity is float || beforeEntity is double || beforeEntity is decimal)
            {
                if (beforeEntity is int)
                {
                    if (Convert.ToInt32(beforeEntity) != Convert.ToInt32(afterEntity))
                    {
                        result.Add(new ColumnChange() { ColumnName = "int", OldValue = Convert.ToInt32(beforeEntity).ToString(), NewValue = Convert.ToInt32(afterEntity).ToString() });
                    }
                }
                else if (beforeEntity is short)
                {
                    if (Convert.ToInt16(beforeEntity) != Convert.ToInt16(afterEntity))
                    {
                        result.Add(new ColumnChange() { ColumnName = "short", OldValue = Convert.ToInt16(beforeEntity).ToString(), NewValue = Convert.ToInt16(afterEntity).ToString() });
                    }
                }
                else if (beforeEntity is long)
                {
                    if (Convert.ToInt64(beforeEntity) != Convert.ToInt64(afterEntity))
                    {
                        result.Add(new ColumnChange() { ColumnName = "long", OldValue = Convert.ToInt64(beforeEntity).ToString(), NewValue = Convert.ToInt64(afterEntity).ToString() });
                    }
                }
                else if (beforeEntity is float)
                {
                    if (Convert.ToSingle(beforeEntity) != Convert.ToSingle(afterEntity))
                    {
                        result.Add(new ColumnChange() { ColumnName = "float", OldValue = Convert.ToSingle(beforeEntity).ToString(), NewValue = Convert.ToSingle(afterEntity).ToString() });
                    }
                }
                else if (beforeEntity is double)
                {
                    if (Convert.ToDouble(beforeEntity) != Convert.ToDouble(afterEntity))
                    {
                        result.Add(new ColumnChange() { ColumnName = "double", OldValue = Convert.ToDouble(beforeEntity).ToString(), NewValue = Convert.ToDouble(afterEntity).ToString() });
                    }
                }
                else if (beforeEntity is decimal)
                {
                    if (Convert.ToDecimal(beforeEntity) == Convert.ToDecimal(afterEntity))
                    {
                        result.Add(new ColumnChange() { ColumnName = "decimal", OldValue = Convert.ToDecimal(beforeEntity).ToString(), NewValue = Convert.ToDecimal(afterEntity).ToString() });
                    }
                }
            }
            else
            {
                var beforeMembers = beforeEntity.GetType().GetProperties();
                var afterMembers = afterEntity.GetType().GetProperties();
                for (int i = 0; i < beforeMembers.Length; i++)
                {
                    var beforeVal = beforeMembers[i].GetValue(beforeEntity, null);
                    var afterVal = afterMembers[i].GetValue(afterEntity, null);
                    var beforeValue = beforeVal == null ? "" : beforeVal.ToString();
                    var afterValue = afterVal == null ? "" : afterVal.ToString();
                    if (beforeValue.Trim() != afterValue.Trim() && beforeVal != afterVal)
                    {
                        result.Add(new ColumnChange() { ColumnName = beforeMembers[i].Name, OldValue = beforeValue, NewValue = afterValue });
                    }
                }
            }
            return result;
        }
    }
}
